package pikes.peak;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import pikes.html.xhtml.Flow;
import pikes.html.xhtml.table.TBody;
import pikes.html.xhtml.table.THead;
import pikes.html.xhtml.table.TR;
import pikes.html.xhtml.table.Table;
import pikes.html.xhtml.text.NonBreakingSpace;
import pikes.peak.Grid;
import pikes.peak.GridDecorator;
import pikes.peak.RequestProperty;
import pikes.peak.test.AssertHtml;
import pikes.peak.test.PikesPeakComponentTestCase;

import org.springframework.web.servlet.support.RequestContext;

public class GridTestCase extends PikesPeakComponentTestCase {

	@Test
	public void emptyRow() throws Exception {
		Grid row = new Grid();

		Table table = new Table(new TR(new NonBreakingSpace()));

		Flow actual = row.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(table, actual);
	}

	@Test
	public void gridDecorator() throws Exception {
		Grid countriesGrid = new Grid(new GridDecorator() {

			public void decorateTable(Table table, Map arg1, RequestContext arg2) throws Exception {
				table.setClazz("stripe ruler");
			}

		});

		Table table = new Table(new TR(new NonBreakingSpace()));
		table.setClazz("stripe ruler");

		Flow actual = countriesGrid.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(table, actual);
	}

	@Test
	public void setHeaders() throws Exception {

		List<RequestProperty<TR>> headerRows = new LinkedList<RequestProperty<TR>>();
		headerRows.add(new RequestProperty<TR>() {

			public TR getValue(Map model, RequestContext requestContext) throws Exception {
				return new TR("boo");
			}

		});

		Grid grid = new Grid();
		grid.setHeaderRows(headerRows);

		Table table = new Table(new TR(new NonBreakingSpace()));
		TR hr = new TR("boo");
		THead head = new THead(hr);
		table.setHeader(head);

		Flow actual = grid.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(table, actual);
	}

	@Test
	public void setRows() throws Exception {

		List<RequestProperty<TR>> rows = new LinkedList<RequestProperty<TR>>();
		rows.add(new RequestProperty<TR>() {

			public TR getValue(Map model, RequestContext requestContext) throws Exception {
				return new TR("boo");
			}

		});

		Grid grid = new Grid();
		grid.setRows(rows);

		TR hr = new TR("boo");
		TBody body = new TBody(hr);
		Table table = new Table(body);

		Flow actual = grid.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(table, actual);
	}

	@Test
	public void setSingleRows1() throws Exception {

		Grid grid = new Grid();
		grid.setRow(new RequestProperty<TR>() {

			public TR getValue(Map model, RequestContext requestContext) throws Exception {
				return new TR("boo");
			}

		});

		TR hr = new TR("boo");
		TBody body = new TBody(hr);
		Table table = new Table(body);

		Flow actual = grid.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(table, actual);
	}
}
